package 数组;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * https://leetcode.cn/problems/majority-element/
 */
public class _169_多数元素 {

    public int majorityElement(int[] nums) {
        int n = nums.length;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }

        Set<Integer> set = map.keySet();
        for (Integer num : set) {
            if (map.get(num) > n / 2) {
                return num;
            }
        }

        return -1;
    }
}
